﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nox.Amuse.Model;
using Nox.Amuse.MVVM;
using System.Windows.Input;
using System.ComponentModel;

namespace Nox.Amuse.ViewModel
{
    public class WorldDataViewModel : ObservableObject, IDataErrorInfo
    {
        private WorldData myWorldDataModel = new WorldData();

        public string DisplayName
        {
            get { return myWorldDataModel.Name; }
            set
            {
                myWorldDataModel.Name = value;
                this.RaisePropertyChanged("DisplayName");
            }
        }

        public string Address
        {
            get { return myWorldDataModel.Address; }
            set
            {
                myWorldDataModel.Address = value;
                this.RaisePropertyChanged("Address");
            }
        }

        public string myPortString;
        public string Port
        {
            get
            {
                if (myPortString == null)
                    myPortString = myWorldDataModel.Port.ToString();
                return myPortString;
            }
            set
            {
                if (value == myPortString)
                    return;
                myPortString = value;
                this.RaisePropertyChanged("Port");
            }
        }

        public WorldDataViewModel()
        { }

        public WorldDataViewModel(WorldData worldData)
        {
            myWorldDataModel = worldData;
        }

        public bool WorldSaved { get; private set; }

        public ICommand SaveCommand
        {
            get
            {
                return mySaveCommand ?? (mySaveCommand = new RelayCommand(
                    () =>
                    {
                        // Close UI and then perform Connection
                        myWorldDataModel.Save();
                        WorldSaved = true;
                        App.Messenger.NotifyColleagues(Messages.CloseDialog, this);
                    },
                    () =>
                    {
                        // Allow command if world is selected
                        int test;
                        if ((!String.IsNullOrEmpty(myWorldDataModel.Name)) &&
                            (!String.IsNullOrEmpty(myWorldDataModel.Address)) &&
                            (String.IsNullOrEmpty(ValidatePort(out test))) &&
                            (myWorldDataModel.Port > 0 && myWorldDataModel.Port < (Int16.MaxValue * 2) + 1))
                            return true;
                        return false;
                    }
                    ));
            }
        }
        private RelayCommand mySaveCommand;

        public string Error
        {
            get { return myWorldDataModel.Error; }
        }

        public string this[string propertyName]
        {
            get
            {
                switch (propertyName)
                {
                    case "Port":
                        int p;
                        string msg = this.ValidatePort(out p);
                        if (!String.IsNullOrEmpty(msg))
                            return msg;
                        myWorldDataModel.Port = p;
                        break;
                    default:
                        break;
                }
                return myWorldDataModel[propertyName];
            }
        }

        private string ValidatePort(out int p)
        {
            string msg = null;
            if (!Int32.TryParse(myPortString, out p))
                msg = "A port number may only contain numbers, no letters or symbols.";
            if (String.IsNullOrEmpty(myPortString))
                msg = "Please provide a port number";
            return msg;
        }

        public void Delete()
        {
            myWorldDataModel.Delete();
        }
    }
}
